#include "MainPageWidget.h"
#include <QApplication>
#include <QPainter>
#include <QDebug>
#include <QResizeEvent>

MainPageWidget::MainPageWidget(QWidget *parent)
    : QWidget(parent)
    , m_navigationVisible(false)
{
    setupUI();
    setupConnections();
}

MainPageWidget::~MainPageWidget()
{
}

NavigationWidget* MainPageWidget::getNavigationWidget() const
{
    return m_navigationWidget;
}

MainOperationWidget* MainPageWidget::getMainOperationWidget() const
{
    return m_mainOperationWidget;
}

WId MainPageWidget::getVideoWindowHandle() const
{
    if (m_mainOperationWidget) {
        return m_mainOperationWidget->videoWindowHandle();
    }
    return 0;
}

void MainPageWidget::setupUI()
{
    setStyleSheet("MainPageWidget{ border-image: url(:/img/images/BG02.png);}");
    setupContentArea();
    setupNavigationContainer();
}

void MainPageWidget::setupContentArea()
{
    m_mainLayout = new QHBoxLayout(this);
    m_mainLayout->setContentsMargins(24, 6, 24, 24);
    m_mainLayout->setSpacing(0);

    m_stackWidget = new QStackedWidget(this);
    m_stackWidget->setStyleSheet("QStackedWidget { background: transparent; border: none; }");
    
    m_smeltingConfigWidget = new SmeltingConfigWidget(m_stackWidget);
    m_mainOperationWidget = new MainOperationWidget(m_stackWidget);
    
    m_stackWidget->addWidget(m_smeltingConfigWidget);
    m_stackWidget->addWidget(m_mainOperationWidget);

    m_mainLayout->addWidget(m_stackWidget);
}

void MainPageWidget::setupNavigationContainer()
{
    m_navigationContainer = new QWidget(this);
    m_navigationContainer->setParent(this);
    m_navigationContainer->raise(); // 确保在最上层

    QHBoxLayout *navigationLayout = new QHBoxLayout(m_navigationContainer);
    navigationLayout->setContentsMargins(0, 0, 0, 0);
    navigationLayout->setSpacing(0);

    m_navigationWidget = new NavigationWidget(m_navigationContainer);
    m_navigationWidget->setFixedWidth(290);
    m_navigationWidget->hide();

    m_toggleNavigationButton = new QPushButton("", m_navigationContainer);
    m_toggleNavigationButton->setObjectName("toggleNavigationButton");
    m_toggleNavigationButton->setFixedWidth(14);
    m_toggleNavigationButton->setCheckable(true);
    m_toggleNavigationButton->setChecked(m_navigationVisible);

    navigationLayout->addWidget(m_navigationWidget);
    navigationLayout->addWidget(m_toggleNavigationButton);

    updateNavigationContainerGeometry();
}

void MainPageWidget::updateNavigationContainerGeometry()
{   
    m_navigationContainer->move(0, 6);
    m_navigationContainer->resize((m_navigationVisible ? m_navigationWidget->width() : 0) + m_toggleNavigationButton->width(), 
        this->height() - (6 + 24));
    m_toggleNavigationButton->setFixedHeight(m_navigationContainer->height());

    // 确保容器始终可见
    m_navigationContainer->show();
    m_navigationContainer->raise();
    m_navigationContainer->activateWindow();
}

void MainPageWidget::setupConnections()
{
    connect(m_navigationWidget, &NavigationWidget::navigationItemClickedByIndex, 
            this, &MainPageWidget::onNavigationItemClickedByIndex);
    connect(m_navigationWidget, &NavigationWidget::stoveExpanded, 
            this, &MainPageWidget::onstoveExpanded);
    connect(m_toggleNavigationButton, &QPushButton::clicked, 
            this, &MainPageWidget::onToggleNavigationButtonClicked);
}

void MainPageWidget::paintEvent(QPaintEvent *event)
{
    QStyleOption opt;
    opt.initFrom(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
    
    QWidget::paintEvent(event);
}

void MainPageWidget::resizeEvent(QResizeEvent *event)
{
    QWidget::resizeEvent(event);
    
    // 始终更新导航容器几何，确保按钮可见
    if (m_navigationContainer) {
        updateNavigationContainerGeometry();
    }
}

void MainPageWidget::onNavigationItemClickedByIndex(int stoveIndex, int pageIndex)
{
    // 根据页面索引显示相应的界面
    switch (pageIndex) {
        case 0: // 冶炼配置界面
            m_stackWidget->setCurrentWidget(m_smeltingConfigWidget);
            if (m_smeltingConfigWidget) {
                m_smeltingConfigWidget->loadDataForStove(stoveIndex);
            }
            break;
        case 1: // 主操作界面
            m_stackWidget->setCurrentWidget(m_mainOperationWidget);
            if (m_mainOperationWidget) {
                m_mainOperationWidget->setCurrentStoveIndex(stoveIndex);
            }
            break;
        case 2: // 冶炼追溯界面
        default:
            break;
    }

    if (pageIndex != 1 && m_mainOperationWidget) {
        m_mainOperationWidget->setCurrentStoveIndex(stoveIndex);
    }
    
    // 根据索引发送信号
    emit navigationSelectedByIndex(stoveIndex, pageIndex);
}

void MainPageWidget::onstoveExpanded(const QString &stoveName)
{
    qDebug() << "stove expanded:" << stoveName;
    // 可以在这里添加炉子展开时的处理逻辑
}

void MainPageWidget::onToggleNavigationButtonClicked()
{
    m_navigationVisible = !m_navigationVisible;
    
    if (m_navigationVisible) {
        m_navigationWidget->show();
    } else {
        m_navigationWidget->hide();
    }
    updateNavigationContainerGeometry();
}
